/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.alianzamedica.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Text;

/**
 * 
 * @author Carlos
 */
public class OXMLTest {

	/*
	 * public void printElement(Document document) throws IOException {
	 * ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintWriter out
	 * = new PrintWriter(baos); document.write(out); out.flush(); String content
	 * = new String(baos.toByteArray()); System.out.println(content); }
	 */

	/**
	 * serializa el documento a un arreglo de bytes.
	 * 
	 * @param document
	 *            documento.
	 * @return arreglo de bytes del documento.
	 * @throws IOException
	 *             excepcion si ocurre error.
	 */
	public byte[] serializeDocument(Document document) throws IOException {
		byte[] data = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintWriter out = new PrintWriter(baos);
		document.setXMLEncoding("ISO-8859-1");
		try {
			document.write(out);
			out.flush();
			data = baos.toByteArray();
		} finally {
			try {
				baos.close();
			} catch (Exception e) {
			}
		}
		return data;

	}

	/**
	 * crea el elemento.
	 * 
	 * @param doctorName
	 *            nombre del doctor.
	 * @param speciality
	 *            especialidad.
	 * @param cedulaProfesional
	 *            cedula profesional.
	 * @param universidad
	 *            universidad.
	 * @param nombreDelPaciente
	 *            nombre del paciente.
	 * @param date
	 *            fecha.
	 * @param edad
	 *            edad.
	 * @param recetas
	 *            arreglo de recetas medicas.
	 * @param direccion
	 *            direccion.
	 * @param telefono
	 *            telefono.
	 * @return documento contenedor de toda la receta.
	 */
	public Document createElement(String doctorName, String speciality,
			String cedulaProfesional, String universidad,
			String nombreDelPaciente, String date, String edad,
			String[] recetas, String direccion, String telefono) {
		DocumentFactory factory = DocumentFactory.getInstance();
		Document document = factory.createDocument();
		Element root = this.createRoot(factory);
		document.add(root);
		Element body = factory.createElement("w:body");
		root.add(body);
		body.add(getElementDoctorName(factory, doctorName));
		body.add(getDoctorSpeciality(factory, speciality));
		body.add(getCedulaProfesional(factory, cedulaProfesional));
		body.add(getUniversidad(factory, universidad));
		body.add(getUniversidad(factory, date));
		body.add(getParagraph(factory, nombreDelPaciente));
		body.add(getParagraph(factory, "Edad: " + edad + " años "));
		int length = recetas.length;
		for (int i = 0; i < length; i++) {
			String receta = recetas[i];
			body.add(getParagraph(factory, receta));
		}
		for (int i = 0; i < 3; i++) {
			// String receta = recetas[i];
			body.add(getParagraph(factory, ""));
		}

		body.add(getParagraph(factory, direccion));
		body.add(getParagraph(factory, "telefono: " + telefono));
		body.add(this.seccionFinal(factory));

		return document;
	}

	private Element getElementDoctorName(DocumentFactory factory,
			String doctorName) {
		Element wpDoctorName = factory.createElement("w:p");
		wpDoctorName.addAttribute("w:rsidR", "00000000");
		wpDoctorName.addAttribute("w:rsidRDefault", "007023E7");
		Element w_pPr = factory.createElement("w:pPr");
		wpDoctorName.add(w_pPr);
		Element wStyle = factory.createElement("w:pStyle");
		wpDoctorName.add(wStyle);
		wStyle.addAttribute("w:val", "RECETA MEDICA");
		Element wCenter = factory.createElement("w:jc");
		wpDoctorName.add(wCenter);
		wCenter.addAttribute("w:val", "center");
		Element wDivId = factory.createElement("w:divId");
		wpDoctorName.add(wDivId);
		wDivId.addAttribute("w:val", "2019194901");
		Element w_rPr = factory.createElement("w:rPr");
		wpDoctorName.add(w_rPr);
		Element wFonts = factory.createElement("w:rFonts");
		w_rPr.add(wFonts);
		wFonts.addAttribute("w:eastAsia", "Times New Roman");
		Element w_lang = factory.createElement("w:lang");
		w_rPr.add(w_lang);
		Element w_r = factory.createElement("w:r");
		wpDoctorName.add(w_r);
		Element w_rPr0 = factory.createElement("w:rPr");
		w_r.add(w_rPr0);
		Element w_rFonts = factory.createElement("w:rFonts");
		w_rPr0.add(w_rFonts);
		w_rFonts.addAttribute("w:eastAsia", "Times New Roman");
		Element w_lang0 = factory.createElement("w:lang");
		w_rPr0.add(w_lang0);
		Element w_t = factory.createElement("w:t");
		w_r.add(w_t);
		w_t.addAttribute("xml:space", "preserve");
		Text medicName = factory.createText("Dr. " + doctorName);
		w_t.add(medicName);

		return wpDoctorName;
	}

	private Element createRoot(DocumentFactory factory) {
		Element root = factory.createElement("w:document");
		root.addAttribute("xmlns:ve",
				"http://schemas.openxmlformats.org/markup-compatibility/2006");
		root.addAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");
		root
				.addAttribute("xmlns:r",
						"http://schemas.openxmlformats.org/officeDocument/2006/relationships");
		root.addAttribute("xmlns:m",
				"http://schemas.openxmlformats.org/officeDocument/2006/math");
		root.addAttribute("xmlns:v", "urn:schemas-microsoft-com:vml");
		root
				.addAttribute("xmlns:wp",
						"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
		root.addAttribute("xmlns:w10", "urn:schemas-microsoft-com:office:word");
		root.addAttribute("xmlns:w",
				"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
		root.addAttribute("xmlns:wne",
				"http://schemas.microsoft.com/office/word/2006/wordml");
		return root;

	}

	private Element getDoctorSpeciality(DocumentFactory factory,
			String speciality) {
		Element wpSpeciality = factory.createElement("w:p");
		wpSpeciality.addAttribute("w:rsidR", "00000000");
		wpSpeciality.addAttribute("w:rsidRDefault", "007023E7");
		Element w_pPr = factory.createElement("w:pPr");
		wpSpeciality.add(w_pPr);
		w_pPr.addAttribute("w:val", "center");
		Element w_divId = factory.createElement("w:divId");
		w_pPr.add(w_divId);
		w_divId.addAttribute("w:val", "1846894495");
		Element w_rPr = factory.createElement("w:rPr");
		w_pPr.add(w_rPr);
		Element w_rFonts = factory.createElement("w:rFonts");
		w_rPr.add(w_rFonts);
		w_rFonts.addAttribute("w:eastAsia", "Times New Roman");
		Element w_b = factory.createElement("w:b");
		w_rPr.add(w_b);
		w_rPr.add(factory.createElement("w:bCs"));
		w_rPr.add(factory.createElement("w:lang"));
		Element w_r = factory.createElement("w:r");
		wpSpeciality.add(w_r);
		Element w_rPr0 = factory.createElement("w:rPr");
		w_r.add(w_rPr0);
		Element w_rFonts0 = factory.createElement("w:rFonts");
		w_rPr0.add(w_rFonts0);
		w_rFonts0.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr0.add(factory.createElement("w:b"));
		w_rPr0.add(factory.createElement("w:bCs"));
		w_rPr0.add(factory.createElement("w:lang"));
		Element w_t = factory.createElement("w:t");
		w_r.add(w_t);
		w_t.addAttribute("xml:space", "preserve");
		Text text = factory.createText("Especialidad : " + speciality + " ");
		w_t.add(text);
		return wpSpeciality;
	}

	private Element getCedulaProfesional(DocumentFactory factory,
			String cedulaProfesional) {
		Element wpCedulaProfesional = factory.createElement("w:p");
		wpCedulaProfesional.addAttribute("w:rsidR", "00000000");
		wpCedulaProfesional.addAttribute("w:rsidRDefault", "007023E7");
		Element w_pPr = factory.createElement("w:rPr");
		Element w_r = factory.createElement("w:r");
		wpCedulaProfesional.add(w_pPr);
		wpCedulaProfesional.add(w_r);
		Element w_jc = factory.createElement("w:jc");
		wpCedulaProfesional.add(w_jc);
		w_jc.addAttribute("w:val", "center");
		Element w_divId = factory.createElement("w:divId");
		w_pPr.add(w_divId);
		w_divId.addAttribute("w:val", "2044742495");
		Element w_rPr = factory.createElement("w:rPr");
		w_pPr.add(w_rPr);
		Element w_rFonts = factory.createElement("w:rFonts");
		w_rPr.add(w_rFonts);
		w_rFonts.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr.add(factory.createElement("w:b"));
		w_rPr.add(factory.createElement("w:bCs"));
		w_rPr.add(factory.createElement("w:lang"));
		Element w_rPr0 = factory.createElement("w:rPr");
		w_r.add(w_rPr0);
		Element w_rFonts0 = factory.createElement("w:rFonts");
		w_rPr0.add(w_rFonts0);
		w_rFonts0.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr0.add(factory.createElement("w:b"));
		w_rPr0.add(factory.createElement("w:bCs"));
		w_rPr0.add(factory.createElement("w:lang"));
		Element w_t = factory.createElement("w:t");
		w_r.add(w_t);
		w_t.addAttribute("xml:space", "preserve");
		Text text = factory.createText("Cédula Profesional "
				+ cedulaProfesional + " ");
		w_t.add(text);
		return wpCedulaProfesional;
	}

	private Element getUniversidad(DocumentFactory factory, String universidad) {
		Element wpUniversidad = factory.createElement("w:p");
		wpUniversidad.addAttribute("w:rsidR", "00000000");
		wpUniversidad.addAttribute("w:rsidRDefault", "007023E7");
		Element w_pPr = factory.createElement("w:pPr");
		Element w_r = factory.createElement("w:r");
		wpUniversidad.add(w_pPr);
		wpUniversidad.add(w_r);
		Element w_jc = factory.createElement("w:jc");
		w_jc.addAttribute("w:val", "center");
		Element w_divId = factory.createElement("w:divId");
		w_divId.addAttribute("w:val", "1180510766");
		Element w_rPr = factory.createElement("w:rPr");
		w_pPr.add(w_jc);
		w_pPr.add(w_divId);
		w_pPr.add(w_rPr);
		Element w_rFonts = factory.createElement("w:rFonts");
		w_rPr.add(w_rFonts);
		w_rFonts.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr.add(factory.createElement("w:lang"));
		Element w_rPr0 = factory.createElement("w:rPr");
		w_r.add(w_rPr0);
		Element w_rFonts0 = factory.createElement("w:rFonts");
		w_rPr0.add(w_rFonts0);
		w_rFonts0.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr0.add(factory.createElement("w:lang"));
		Element w_t = factory.createElement("w:t");
		w_r.add(w_t);
		w_t.addAttribute("xml:space", "preserve");
		Text text = factory.createText(universidad);
		w_t.add(text);
		return wpUniversidad;
	}

	private Element getParagraph(DocumentFactory factory, String paragraph) {
		Element wpUniversidad = factory.createElement("w:p");
		wpUniversidad.addAttribute("w:rsidR", "00000000");
		wpUniversidad.addAttribute("w:rsidRDefault", "007023E7");
		Element w_pPr = factory.createElement("w:pPr");
		Element w_r = factory.createElement("w:r");
		wpUniversidad.add(w_pPr);
		wpUniversidad.add(w_r);
		Element w_jc = factory.createElement("w:jc");
		w_jc.addAttribute("w:val", "center");
		Element w_divId = factory.createElement("w:divId");
		w_divId.addAttribute("w:val", "1180510766");
		Element w_rPr = factory.createElement("w:rPr");
		w_pPr.add(w_jc);
		w_pPr.add(w_divId);
		w_pPr.add(w_rPr);
		Element w_rFonts = factory.createElement("w:rFonts");
		w_rPr.add(w_rFonts);
		w_rFonts.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr.add(factory.createElement("w:lang"));
		Element w_rPr0 = factory.createElement("w:rPr");
		w_r.add(w_rPr0);
		Element w_rFonts0 = factory.createElement("w:rFonts");
		w_rPr0.add(w_rFonts0);
		w_rFonts0.addAttribute("w:eastAsia", "Times New Roman");
		w_rPr0.add(factory.createElement("w:lang"));
		Element w_t = factory.createElement("w:t");
		w_r.add(w_t);
		w_t.addAttribute("xml:space", "preserve");
		Text text = factory.createText(paragraph);
		w_t.add(text);
		return wpUniversidad;
	}

	private Element seccionFinal(DocumentFactory factory) {
		Element w_sectPr = factory.createElement("w:sectPr");
		w_sectPr.addAttribute("w:rsidR", "007023E7");
		Element w_pgSz = factory.createElement("w:pgSz");
		w_pgSz.addAttribute("w:w", "12240");
		w_pgSz.addAttribute("w:h", "15840");
		Element w_pgMar = factory.createElement("w:pgMar");
		w_pgMar.addAttribute("w:top", "1417");
		w_pgMar.addAttribute("w:right", "1701");
		w_pgMar.addAttribute("w:bottom", "1417");
		w_pgMar.addAttribute("w:left", "1701");
		w_pgMar.addAttribute("w:header", "708");
		w_pgMar.addAttribute("w:footer", "708");
		w_pgMar.addAttribute("w:gutter", "0");
		Element w_cols = factory.createElement("w:cols");
		w_cols.addAttribute("w:space", "708");
		Element w_docGrid = factory.createElement("w:docGrid");
		w_docGrid.addAttribute("w:linePitch", "360");
		w_sectPr.add(w_pgSz);
		w_sectPr.add(w_pgMar);
		w_sectPr.add(w_cols);
		w_sectPr.add(w_docGrid);

		return w_sectPr;
	}
}
